package basic.arch.component.lock.distributed.mysql.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import basic.arch.component.lock.LockRuntimeException;
import basic.arch.component.lock.distributed.mysql.mapper.Lock;
import basic.arch.component.lock.distributed.mysql.mapper.LockMapper;
@Service("lockService")
public class LockServiceImpl implements LockService{

    
    @Autowired
    private LockMapper lockMapper;

    @Override
    public boolean insertIfAbsent(String type) {
        Long isExist = lockMapper.selectIdByType(type);
        if(isExist==null||isExist==0){
            Lock lock = new Lock();
            lock.setType(type);
            int flag = lockMapper.insert(lock);
            if(flag!=1){
                throw new LockRuntimeException("插入失败");
            }
            return true;
        }else{
            return false;
        }
        
    }

    @Override
    public long selectLastId(String type) {
        return lockMapper.selectIdByType(type);
    }

    @Override
    public void deleteLock(String type) {
        int flag = lockMapper.deleteByType(type);
        if(flag!=1){
            throw new LockRuntimeException("删除失败");
        }
    }

    @Override
    public List<Lock> findAll() {
        return lockMapper.findAll();
    }
}
